<template>
  <div class="dashboard-container">
    <div class="app-container">
      <UploadExcel :on-success="onSuccess" />
    </div>
  </div>
</template>

<script>
import { batchStaffAPI } from '@/api/employees'
export default {
  methods: {
    // 导入数据
    async onSuccess(data) {
      const aaa = data.results.map(item => {
        const obj = []
        for (const i in item) {
          obj.push(item[i])
        }
        obj[2] = new Date(this.formatExcelTime(obj[2]))
        return { username: obj[0], mobile: obj[1], timeOfEntry: obj[2], correctionTime: obj[3], workNumber: obj[4] }
      })
      await batchStaffAPI(aaa)
      this.$$message.success('导入成功')
      this.$router.back()
    },
    // 时间戳转化插件
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }
  }
}
</script>

<style>
</style>
